<?php

namespace App\Http\Controllers\Admin;

use App\Exceptions\SeriesExistsException;
use App\Models\Series;

class SeriesController extends Controller
{
    /*
     * @api get /course-api/a/semesters 学期列表
     * @version 0.0.1
     * @group admin
     *
     * @param query int [pageSize] 页数，默认15
     * @param query int [page] 页码，默认1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function index()
    {
        $this->validate($this->request, [
            "pageSize" => "integer|max:100|min:5",
            "page" => "integer|min:1"
        ]);
        $page = intval($this->request->input("page", 1));
        $pageSize = intval($this->request->input("pageSize", 15));
        $query = Series::query();
        $total = $query->count();
        $semesters = $query->orderBy('id', 'desc')->forPage($page, $pageSize)
            ->get();
        $items = [];
        if ($total) {
            foreach ($semesters as $semester) {
                $items[] = [
                    'id' => $semester->id,
                    'version' => $semester->getVersion(),
                    'isPublished' => $semester->getPublished(),
                    'createdAt' => $semester->getCreatedAt(),
                    'updatedAt' => $semester->getUpdatedAt()
                ];
            }
        }
        return $this->sendSuccessJson(['total'=>$total, 'items'=>$items]);
    }

    /*
     * @api post /course-api/a/semesters 新增学期
     * @version 0.0.1
     * @group admin
     *
     * @param body int version 学期数
     * @param body date startTime 开始时间
     * @param body date endTime 结束时间
     * @param body int [copyId] 副本ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function store()
    {
        $this->validate($this->request, [
            'version' => 'required',
            'startTime' => 'required',
            'endTime' => 'required'
        ]);
        $version = $this->request->input('version');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime');
        $copyId = $this->request->input('copyId');
        if (Series::where('version', $version)->exists()) {
            throw new SeriesExistsException();
        }
        $semester = new Series();
        $semester->setVersion($version);
        $semester->setStartTime($startTime);
        $semester->setEndTime($endTime);
        $semester->save();
        if ($copyId) {
            $courses = Series::find($copyId)->courses;
            foreach ($courses as $course) {
                $newCourse = $course->replicate();
                $newCourse->setSemesterId($semester->id);
                $newCourse->save();
                $lessons = $course->lessons;
                foreach ($lessons as $lesson) {
                    $newLesson = $lesson->replicate();
                    $newLesson->setCourseId($course->id);
                    $newLesson->save();
                }
            }
        }
        return $this->sendSuccessJson($semester->id);
    }

    /*
     * @api put /course-api/a/semesters/{semesterId} 更新学期
     * @version 0.0.1
     * @group admin
     *
     * @param body int version 学期数
     * @param body date startTime 开始时间
     * @param body date endTime 结束时间
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function update($id)
    {
        $this->request->merge(['id' => $id]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'version' => 'required|integer',
            'startTime' => 'required',
            'endTime' => 'required'
        ]);
        $version = $this->request->input('version');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime');

        $semester = Series::find($id);
        $semester->setVersion($version);
        $semester->setStartTime($startTime);
        $semester->setEndTime($endTime);
        $semester->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api post /course-api/a/semesters/{semesterId}/publish 更新发布状态
     * @version 0.0.1
     * @group admin
     *
     * @param body int isPublished 是否发布，0，1
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":""
     * }
     */
    public function publish($id)
    {
        $this->request->merge(['id' => $id]);
        $this->validate($this->request, [
            'id' => 'required|integer',
            'isPublished' => 'required|integer|in:0,1',
        ]);
        $isPublished = $this->request->input('isPublished');
        /** @var Series $semester */
        $semester = Series::find($id);
        $semester->setPublished($isPublished);
        $semester->save();
        return $this->sendSuccessJson();
    }

    /*
     * @api delete /course-api/a/semesters/{semesterId} 删除学期
     * @version 0.0.1
     * @group admin
     *
     * @param path int semesterId 学期ID
     *
     * @response 200 正常结果 {
     *  "code":0,
     *  "msg":"",
     *  "data":{
     *    "total":10,
     *    "items":[]
     * }
     * }
     */
    public function destroy($id)
    {
        Series::destroy(intval($id));
        return $this->sendSuccessJson();
    }
}
